﻿using LBSoft.Business.DAL;
using LBSoft.Business;
using LBSoft.Common.DotNetCode;
using LBSoft.Common.DotNetJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
using Newtonsoft.Json;
using System.Collections;
using LBSoft.Common.DotNetExt;

namespace LBSoft.HplusAdmin.Business.Home
{
    /// <summary>
    /// Home 的摘要说明
    /// </summary>
    public class Home : IHttpHandler
    {
        HttpContext con = null;
        mJsonResult json = new mJsonResult();
        Common_Dal systemidao = new Common_Dal();
        LogHelper log = new LogHelper();

        public void ProcessRequest(HttpContext context)
        {
            con = context;

            switch (con.Request["PostType"])
            {
                case "get": GetData(); break;
            }
        }

        #region 获取
        private void GetData()
        {
            switch (con.Request["ActionType"])
            {
                case "HomeStaInfo": GetHomeStaInfo(); break; //获取统计信息
                case "ProjectList": GetProjectList(); break; //获取地图项目信息
            }
        }

        /// <summary>
        /// 获取统计信息
        /// </summary>
        private void GetHomeStaInfo()
        {
            Hashtable ht = new Hashtable();

            string today = DateTime.Now.ToString("yyyy-MM-dd");
            int projectCount = systemidao.ExecuteScalar($@"SELECT COUNT(0) FROM dbo.T_Project").ToInt32();
            int personCount = systemidao.ExecuteScalar($@"SELECT COUNT(0) FROM dbo.T_Person").ToInt32();
            int todayLoginCount = systemidao.ExecuteScalar($@"SELECT COUNT(0) FROM dbo.SYS_LoginLog WHERE LoginStatus='登录成功' AND CONVERT(VARCHAR(50),LoginDate,23)='{today}'").ToInt32();
            int totalLoginCount = systemidao.ExecuteScalar($@"SELECT COUNT(0) FROM dbo.SYS_LoginLog WHERE LoginStatus='登录成功'").ToInt32();
            ht["ProjectCount"] = projectCount;
            ht["PersonCount"] = personCount;
            ht["TodayLoginCount"] = todayLoginCount;
            ht["TotalLoginCount"] = totalLoginCount;

            con.Response.Write(JsonConvert.SerializeObject(ht));
        }

        /// <summary>
        /// 获取地图项目信息
        /// </summary>
        private void GetProjectList()
        {
            DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder($@"SELECT * FROM dbo.T_Project"));
            con.Response.Write(JsonConvert.SerializeObject(dt));
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}